/**
 *
 * @param arr 原formJson数据
 * @param keyName 需要修改的name
 * @param value 更新的值
 */
export function updateFormJson(arr, keyName = 'id', value = {}) {
    return arr.reduce((item, nextItem) => {
        if (nextItem.name === keyName) {
            for (let i in value) {
                if (i.indexOf('arr:') === -1) {
                    nextItem[i] = value[i]
                } else {
                    nextItem = treeStrGroup(nextItem, i, value[i])
                }
            }
        }
        item.push(nextItem)
        return item
    }, [])
}

/**
 * 修改深处value值
 * @param obj 对象
 * @param keys key值:a.b.c
 * @param value
 */
export function treeStrGroup(obj, keys, value) {
    let strs = keys.substring(keys.indexOf('arr:') + 4, keys.length).split('.')
    strs = strs.reverse().reduce((item, next) => {
        item = { [next]: item }
        return item
    }, value)

    for (let i in obj) {
        for (let x in strs) {
            if (i === x) {
                obj[i] = Object.assign(obj[i], strs[x])
            }
        }
    }
    return obj
}

/**
 * 编辑value值
 * @param arr FormJson数据
 * @param row value对象
 * @constructor
 */
export function RowConcatFormJson(arr, row) {
    return arr.map(item => {
        item.value = row[item.name]
        return item
    })
}

export function getTableStyle(col, height = 230) {
    let obj = { x: 0, y: 0 }
    obj.y = window.outerHeight - height - 200
    if (col && col.length > 0) {
        col.forEach(item => {
            obj.x += item.width || 150
        })
        obj.x += 200
    } else {
        obj.x = 1000
    }
    return obj
}
